
local g_enum     = require("modules.business_welfare.welfare_enum")
local ns_account = require("modules.business_welfare.welfare_bridge")
local code_conf  = require("modules.business_welfare.welfare_code")
local log               = require('modules.commhm.log')
local uu         = require("modules.commhm.utils")
local op_log     = require("modules.business_welfare.welfare_op_log")

local pairs      = pairs
local math       = math

local ngx = ngx

local FOO = {
    _VERSION = '0.12.1',
}
local error_code = code_conf.error_code


--! @brief   坐骑抽奖券购买
FOO.buy_mount_lotterites = function()
    local buy_times = tonumber(ngx.ctx.m_params.buy_times) or 0
    --扣除消耗物品
    if buy_times < 1 or buy_times > 100 then
        return error_code.ERROR_PARAM
    end

    local num = buy_times * 20 --单个抽奖券20个迷你币
    local action = 'buy_mount_lotterites'

    local items = {
        {id=g_enum.item_id.mount_lotterites, num=buy_times},
        {id=g_enum.item_id.firework, num=buy_times}
    }

    local ret = ns_account.private_account.item_remove(g_enum.item_id.minibi, num, action, {items=items})
    if not ret then
        return error_code.ERROR_DEL_ITEM
    end

    ret = ns_account.private_account.items_add(items, action)
    if not ret then
        log.day_list("welfare_add_item", "buy_mount_lotterites", ngx.ctx.m_params.uin, uu.to_str(items), uu.now(), uu.to_str(ret))
        return error_code.ERROR_ADD_ITEM
    end
    return error_code.COMMON_SUCCESS
end


--! @brief   扭蛋抽奖券购买
FOO.buy_gashapon_lotterites = function()
    local buy_times = tonumber(ngx.ctx.m_params.buy_times) or 0
    --扣除消耗物品
    if buy_times < 1 or buy_times > 100 then
        return error_code.ERROR_PARAM
    end

    local num = buy_times * 68 --单个抽奖券20个迷你币
    local action = 'welfare_lottery'

    local items = {
        {id=g_enum.item_id.star_lottery, num=buy_times},
        {id=g_enum.item_id.luckly_firework, num=buy_times},
    }

    local ret = ns_account.private_account.item_remove(g_enum.item_id.minibi, num, action, {items=items})
    if not ret then
        return error_code.ERROR_DEL_ITEM
    end

    ret = ns_account.private_account.items_add(items, action)
    if not ret then
        log.day_list("welfare_add_item", "buy_gashapon_lotterites", ngx.ctx.m_params.uin, uu.to_str(items), uu.now(), uu.to_str(ret))
        return error_code.ERROR_ADD_ITEM
    end
    local log_info = {
        action = "welfare_lottery_star_lotterites",
        cost_id = g_enum.item_id.minibi,
        cost_num = 68,
        buy_times = buy_times,
        items = items,
    }
    FOO.write_op_log(log_info)

    return error_code.COMMON_SUCCESS
end


FOO.write_op_log = function(loginfo)
    op_log.record("welfare_gift", loginfo.action, loginfo)
end


return FOO